package aorey.database.config;

import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 翻页配置
 * @author csq
 */
public class DataConfigPage
{
    public String id = "";
    public int cache = 120;
    public String data_source = "";
    public String cache_type = "redis";
    public String class_name = "";
    public String method_name = "";
    public String sql = "";
    // 默认分页数量
    public int size = 20;

    /**
     *
     * @param configFile
     * @param server
     * @throws DocumentException
     */
    public DataConfigPage(String configFile) throws DocumentException 
    {
        File config = new File(configFile);
        if (config.exists()) {
            SAXReader reader = new SAXReader();
            Document document = reader.read(config);
            // 根节点
            Element node = document.getRootElement();
            // sql语句的
            Element sqlNode = node.elements().get(0);
            
            this.id = sqlNode.attributeValue("id");
            this.data_source = sqlNode.attributeValue("data_source");
            if (null == sqlNode.attributeValue("cache")) {
                this.cache = -1;
            }
            else
            {
                this.cache = Integer.parseInt(sqlNode.attributeValue("cache"));
            }
            this.cache_type = sqlNode.attributeValue("cache_type");
            this.class_name = sqlNode.attributeValue("class");
            this.method_name = sqlNode.attributeValue("method");
            this.sql = sqlNode.getTextTrim(); 
            this.size = Integer.parseInt(sqlNode.attributeValue("size"));           
            if (this.size <= 0 || this.size > 100)
            {
                this.size = 20;
            }
        }
    }
    
}
